﻿using System;
using System.Configuration;
using System.IO;
using RemoteTestRunner.BusinessLogic.Properties;
using System.ComponentModel;

namespace RemoteTestRunner.BusinessLogic
{
    public class RtrSettingsClient : IRtrSettings, INotifyPropertyChanged
    {
        public RtrSettingsClient()
        {

        }

        #region IRtrSettings Members

        #region DataPath (INotifyPropertyChanged Property)

        public string DataPath
        {
            get { return Path.GetFullPath(Settings.Default.DataPath); }
            set
            {
                if (Settings.Default.DataPath != value)
                {
                    Settings.Default.DataPath = value;
                    OnPropertyChanged("DataPath");
                }
            }
        }

        #endregion

        #region WorkingFolder (INotifyPropertyChanged Property)

        public string WorkingFolder
        {
            get
            {
                var path = Path.GetFullPath(Settings.Default.WorkingFolder);

                if (!Directory.Exists(path))
                {
                    try
                    {
                        Directory.CreateDirectory(path);

                    }
                    catch (Exception ex)
                    {
                        //TODO Ignore error
                    }
                }

                return path;
            }
            set
            {
                if (Settings.Default.WorkingFolder != value)
                {
                    Settings.Default.WorkingFolder = value;
                    OnPropertyChanged("WorkingFolder");
                }
            }
        }

        #endregion

        #region MSTestPath (INotifyPropertyChanged Property)

        public string MsTestPath
        {
            get { return Settings.Default.MSTestPath; }
            set
            {
                if (Settings.Default.MSTestPath != value)
                {
                    Settings.Default.MSTestPath = value;
                    OnPropertyChanged("MSTestPath");
                }
            }
        }

        #endregion

        #region TestTimeoutSeconds (INotifyPropertyChanged Property)

        public int TestTimeoutSeconds
        {
            get { return Settings.Default.TestTimeoutSeconds; }
            set
            {
                if (Settings.Default.TestTimeoutSeconds != value)
                {
                    Settings.Default.TestTimeoutSeconds = value;
                    OnPropertyChanged("TestTimeoutSeconds");
                }
            }
        }

        #endregion

        #region UseRunnerService (INotifyPropertyChanged Property)

        public bool UseRunnerService
        {
            get { return Settings.Default.UseRunnerService; }
            set
            {
                if (value != Settings.Default.UseRunnerService)
                {
                    Settings.Default.UseRunnerService = value;
                    OnPropertyChanged("UseRunnerService");
                }
            }
        }

        #endregion

        public bool FilterTests
        {
            get { return Properties.Settings.Default.FilterTests; }
            set { Properties.Settings.Default.FilterTests = value; }
        }

        #region RunnerServiceUrl (INotifyPropertyChanged Property)

        public string RunnerServiceUrl
        {
            get { return Settings.Default.RunnerServiceUrl; }
            set
            {
                if (Settings.Default.RunnerServiceUrl != value)
                {
                    Settings.Default.RunnerServiceUrl = value;
                    OnPropertyChanged("RunnerServiceUrl");
                }
            }
        }

        #endregion

        #endregion

        #region INotifyPropertyChanged values

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Save    

        public event EventHandler SettingsSaved;

        public void Save()
        {
            Properties.Settings.Default.Save();
            OnSettingsSaved();
        }

        protected void OnSettingsSaved()
        {
            if (SettingsSaved != null)
            {
                SettingsSaved(this, EventArgs.Empty);
            }
        }


        #endregion

        public void Cancel()
        {
            Properties.Settings.Default.Reload();
        }
    }
}